package test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class WindDataSplit2 {

    public static void main(String[] args) throws Exception {
        split("FJ001");
    }

    private static void split(String num) throws Exception {
        File directory = new File("/Users/hym/Desktop/ML/WindPower/file/F1003_install/FJ001_new/");
        File[] files = directory.listFiles();
        if (files == null) {
            return;
        }

        String name = "/Users/hym/Desktop/ML/WindPower/file/haiwanshi_" + num + "_test.csv";
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(name), StandardCharsets.UTF_8));

        List<File> fileList = Stream.of(files).filter(f -> f.getName().contains(num))
                .sorted(Comparator.comparing(File::getName))
                .collect(Collectors.toList());
        JSONObject first = null;
        List<String> keys = null;
        try {
            for (int no = 0; no < fileList.size(); no++) {
                File file = fileList.get(no);
                System.out.println(file.getName());
                if (!file.getName().contains(num) ) {
                    continue;
                }
                FileInputStream fileInputStream = new FileInputStream(file);
                int fileLength = ((Long)file.length()).intValue();
                byte[] fileContent = new byte[fileLength];
                int readLength = fileInputStream.read(fileContent);
                if (readLength == fileLength) {
                    String content = new String(fileContent);
                    JSONObject jsonContent = JSON.parseObject(content);
                    JSONArray data = getData(jsonContent);
                    if (data.size() == 0) {
                        continue;
                    }

                    if (first == null) {
                        first = data.getJSONObject(0);
                        out.write("风机编号");
                        out.write(",");
                        keys = new ArrayList<>(first.keySet());
                        for (int i = 0; i < keys.size(); i++) {

                            if (keys.get(i).equals("time")) {
                                out.write("时间");
                            } else if (keys.get(i).equals("高后备保护装置,UAB")) {
                                out.write("UAB");
                            } else if (keys.get(i).equals("高后备保护装置,UA")) {
                                out.write("UA");
                            } else {
                                out.write(keys.get(i));
                            }
                            if (i != keys.size() - 1) {
                                out.write(",");
                            }
                        }
                        out.newLine();
                    }

                    for (int i = 0; i < data.size(); i++) {
                        JSONObject element = data.getJSONObject(i);
                        if (element.keySet().size() < keys.size()) {
                            continue;
                        }
                        out.write(num);
                        out.write(",");
                        for (int j = 0; j < keys.size(); j++) {
                            try {
                                out.write(element.getString(keys.get(j)));
                            } catch (Exception e) {
                                System.out.println(element);
                                System.out.println(keys.get(j));
                                System.out.println(element.getString(keys.get(j)));
                            }

                            if (j != keys.size() - 1) {
                                out.write(",");
                            }
                        }
                        out.newLine();
                    }



                } else {
                    System.out.println("can not read content");
                }
            }

        } finally {
            out.flush();
            out.close();
        }
    }

    private static JSONArray getData(JSONObject content) {

        try {
            return content.getJSONObject("data").getJSONArray("wts").getJSONObject(0).getJSONArray("datas");
        } catch (Exception e) {
            return new JSONArray();
        }
    }

}
